<template>
	<view>
		<user-info v-if="hasLogin" :teamOrderCount="teamOrderCount"></user-info>
		<login v-if="!hasLogin" @login="getTeamOrderCount"></login>
		<!-- #ifdef H5 -->
		<np-tabbar path="/pages/tabbar/user"></np-tabbar>
		<!-- #endif -->
	</view>
</template>

<script>
	import base from '@/common/base.js';
	import login from './components/login.vue'
	import userInfo from './components/userInfo.vue'
	export default {
		mixins: [base],
		components: {login, userInfo},
		data(){
			return {
				teamOrderCount: {}
			}
		},
		onLoad() {

		},
		onShow() {
			this.getTeamOrderCount();
		},
		methods: {
			getTeamOrderCount() {
				if (!this.hasLogin || (this.globalConfig.setting && this.globalConfig.setting.is_rebate == 0)) {
					return;
				}
				uni.getStorage({
					key: "ORDERCOUNT",
					success: (res) => {
						let result = JSON.parse(res.data);
						this.teamOrderCount = result;
					}
				});
				let time = Date.parse(new Date()) / 1000;
				if (this.orderCountTime && time - this.orderCountTime < 60 * 5) {
					return;
				}
				this.$http.get('api/user/order-count').then((res) => {
					this.teamOrderCount = res;
					uni.setStorage({
						key: "ORDERCOUNT",
						data: JSON.stringify(res)
					});
					let userInfo = this.userInfo;
					userInfo.level = res.level;
					if (res.relation_id) {
						userInfo.relation_id = res.relation_id;
					}
					this.updateUserInfo(userInfo);
					this.setOrderCountTime();
				}).catch((error) => {});
			},
		}
	}
</script>

<style>

</style>
